<?php
namespace app\api\controller\v1;

use app\api\service\TokenService;
use app\api\service\UserService;
use app\model\User;

class MyInfo
{

    /**
     *我的页面
     * */
    public function index()
    {
        try{
            $uid = TokenService::getCurrentTokenVar('uid');
            $user = User::find($uid);
            return json([
                'code'=>0,
                'data'=>[
                    'sign_count'=> 100+$user->join_times,
                    'joined_count'=> $user->sign_times,
                ]
            ]);
        }catch (\Exception $exception){
            return json([
                'code'=>1,
                'msg'=>$exception->getMessage()
            ]);
        }

    }

    /**
     * 参与 打卡
    */
    public function joinPay(){
        try{
            $uid = TokenService::getCurrentTokenVar('uid');
            $join_res = UserService::joinPay($uid);

            return json([
                'code'=>0,
            ]);
        }catch (\Exception $exception){
            return json([
                'code'=>1,
            ]);
        }
    }

    /**
     * 打卡签到
    */
    public function sign(){
        try{
            $today = date('Y-m-d');
            $timestamp_start = $today.' 5:00';
            $timestamp_end = $today.' 8:00';
            $timestamp_start = strtotime($timestamp_start);
            $timestamp_end = strtotime($timestamp_end);
            $timeNow = time();
            if($timeNow>$timestamp_end || $timeNow<$timestamp_start ){
                throw new \Exception('不在签到时间范围内！');
            }
            $uid = TokenService::getCurrentTokenVar('uid');
            UserService::sign($uid);
            return json([
                'code'=>0
            ]);
        }catch (\Exception $exception){
            return json([
                'code'=>1,
                'msg'=>$exception->getMessage()
            ]);
        }
    }

}
